package com.spelet.screens;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.utils.TimeUtils;
import com.spelet.GamePreferences;
import com.spelet.Main;
import com.spelet.Renderer;
import com.spelet.StaticSprites;
import com.spelet.StaticTextures;

public class LevelSelect extends Screen{
	public Main main;
	public OrthographicCamera cam;
	public Sprite bg;
	public Sprite[] levels;
	public Sprite[] levelbackgrounds;
	public Sprite border;
	public Sprite borderLocked;
	public Sprite exit;
	float deltaX;
	float startX;
	float startY;
	float draggedX;
	float draggedY;
	long touchTime;
	Vector3 translationVector;
	int timers[];
	public LevelSelect(Main m) {
		deltaX = 0;
		timers = new int[8];
		translationVector = new Vector3();
		main = m;
		cam = new OrthographicCamera();
		cam.zoom = (float)64f*10f/(float)Gdx.graphics.getWidth();
		cam.position.set(Gdx.graphics.getWidth()*cam.zoom/2,Gdx.graphics.getHeight()*cam.zoom/2,0);
		cam.update();
		bg = StaticSprites.mainbg[0];
		levels = new Sprite[8];
		levels[0] = StaticSprites.moveBlockBase;
		levels[1] = StaticSprites.beeAnimation[0];
		levels[2] = StaticSprites.dirt[0];
		levels[3] = StaticSprites.mergeBlockBaseAnim[0];
		levels[4] = StaticSprites.buttonpressed[0];
		levels[5] = StaticSprites.dooropening[0];
		levels[6] = StaticSprites.boulderBase;
		levels[7] = StaticTextures.levelSelect.createSprite("rageface");

		levelbackgrounds = new Sprite[levels.length];
		levelbackgrounds[0] = StaticTextures.levelSelect.createSprite("left");
		for(int i = 1; i < levelbackgrounds.length-1; i++){
			levelbackgrounds[i] = StaticTextures.levelSelect.createSprite("center");
		}
		levelbackgrounds[levelbackgrounds.length-1] = StaticTextures.levelSelect.createSprite("right");
		border = StaticTextures.levelSelect.createSprite("levelborder");
		borderLocked = StaticTextures.levelSelect.createSprite("levelborderlocked");
		exit = StaticSprites.exit;



	}
	public void update() {
		for (int i = 0; i < levelbackgrounds.length;i++) {
			timers[i] += Gdx.graphics.getDeltaTime()*1000;
		}
		int step = 40;
		if (timers[1] <= step) {
			levels[1] = StaticSprites.beeAnimation[0];
		} else if (timers[1] <= step*2){
			levels[1] = StaticSprites.beeAnimation[1];
		} else if (timers[1] <= step*3){
			levels[1] = StaticSprites.beeAnimation[2];
		} else if (timers[1] <= step*4){
			levels[1] = StaticSprites.beeAnimation[3];
		} else if (timers[1] <= step*5){
			levels[1] = StaticSprites.beeAnimation[4];
		} else if (timers[1] <= step*6){
			levels[1] = StaticSprites.beeAnimation[5];
		} else {
			levels[1] = StaticSprites.beeAnimation[6];
			timers[1] =0;
		}


		step = 200;
		timers[3] += Gdx.graphics.getDeltaTime()*1000;
		if (timers[3] <= step) {
			levels[3] = StaticSprites.mergeBlockBaseAnim[0];
		} else if (timers[3] <= step*2){
			levels[3] = StaticSprites.mergeBlockBaseAnim[1];
		} else if (timers[3] <= step*3){
			levels[3] = StaticSprites.mergeBlockBaseAnim[2];
		} else if (timers[3] <= step*4){
			levels[3] = StaticSprites.mergeBlockBaseAnim[3];
		} else if (timers[3] <= step*5){
			levels[3] = StaticSprites.mergeBlockBaseAnim[4];
		} else if (timers[3] <= step*6){
			levels[3] = StaticSprites.mergeBlockBaseAnim[5];
		} else if (timers[3] <= step*7){
			levels[3] = StaticSprites.mergeBlockBaseAnim[4];
		} else if (timers[3] <= step*8){
			levels[3] = StaticSprites.mergeBlockBaseAnim[3];
		} else if (timers[3] <= step*9){
			levels[3] = StaticSprites.mergeBlockBaseAnim[2];
		} else {
			levels[3] = StaticSprites.mergeBlockBaseAnim[1];
			timers[3] = 0;
		}

	}
	public void render() {
		Renderer.getInstance().renderLevelSelect(this);
	}
	public void renderLevels(SpriteBatch sb) {
		levelbackgrounds[0].setPosition(-5+deltaX,100);
		levelbackgrounds[0].draw(sb);
		for (int i =1; i < levelbackgrounds.length-1; i++) {
			levelbackgrounds[i].setPosition(100*i+deltaX,100);
			levelbackgrounds[i].draw(sb);
		}
		levelbackgrounds[levelbackgrounds.length-1].setPosition((levelbackgrounds.length-1)*100+deltaX,100);
		levelbackgrounds[levelbackgrounds.length-1].draw(sb);
		for (int i = 0; i < levels.length; i++) {
			levels[i].setSize(64,64);
			levels[i].setColor(1,1,1,1);
			levels[i].setPosition(100*i+deltaX,100);
			levels[i].draw(sb);
			if (GamePreferences.unlocked[i]) {
				border.setPosition(levels[i].getX(),levels[i].getY());
				border.draw(sb);
			} else {
				borderLocked.setPosition(levels[i].getX(),levels[i].getY());
				borderLocked.draw(sb);
			}
		}
		
		exit.setPosition(0,0);
		
		exit.draw(sb);
	}
	public void enter() {
		Gdx.input.setInputProcessor(this);
		Gdx.input.setCatchBackKey(true);
	}
	@Override
	public boolean keyDown(int arg0) {
		if (Input.Keys.BACK == arg0) {
			main.setScreen(Main.SCREEN_MAINMENU);
		}
		return false;
	}
	@Override
	public boolean keyTyped(char arg0) {
		// TODO Auto-generated method stub
		return false;
	}
	@Override
	public boolean keyUp(int arg0) {
		// TODO Auto-generated method stub
		return false;
	}
	@Override
	public boolean scrolled(int arg0) {
		// TODO Auto-generated method stub
		return false;
	}
	@Override
	public boolean touchDown(int arg0, int arg1, int arg2, int arg3) {
		touchTime = TimeUtils.millis();
		translationVector.set(arg0,arg1,0);
		cam.unproject(translationVector);
		if (exit.getBoundingRectangle().contains(translationVector.x,translationVector.y))
			main.setScreen(Main.SCREEN_MAINMENU);
		startX = (int) translationVector.x;
		startY = (int) translationVector.y;
		draggedX = startX;
		draggedY = startY;
		return false;
	}
	@Override
	public boolean touchDragged(int arg0, int arg1, int arg2) {		
		translationVector.set(arg0,arg1,0);
		cam.unproject(translationVector);
		deltaX += ((int)translationVector.x)-(int) draggedX;
		if (deltaX > 200)
			deltaX = 200;
		if (deltaX < -500)
			deltaX = -500;
		draggedX = translationVector.x;
		draggedY = translationVector.y;
		return false;
	}
	@Override
	public boolean touchMoved(int arg0, int arg1) {
		// TODO Auto-generated method stub
		return false;
	}
	@Override
	public boolean touchUp(int arg0, int arg1, int arg2, int arg3) {
		translationVector.set(arg0,arg1,0);
		cam.unproject(translationVector);
		if (touchTime-TimeUtils.millis() < 500) {
			for (int i = 0; i < levels.length; i++) {
				if (GamePreferences.unlocked[i]) {
					if (levels[i].getBoundingRectangle().contains(startX,startY) && levels[i].getBoundingRectangle().contains(translationVector.x,translationVector.y)) {
						((Game) main.getState(Main.SCREEN_GAME)).setLevel(i+1);
						main.setScreen(Main.SCREEN_GAME);
					}
				}
			}
		}
		touchTime = 0;
		return false;
	}
}
